package com.engine.jucailinkq.attendance.vacation.job.holidaygeneration.tactics;


import com.engine.jucailinkq.attendance.enums.StartingUnitEnum;
import com.engine.jucailinkq.attendance.vacation.util.VocationCommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.*;

/**
 * 一次发完所有额度
 */
public class OneTimePayMentWay implements HolidayGenerationWay{
    @Override
    public boolean support(int personClassfiy) {
        if (personClassfiy == 3){
            return true;
        }
        return false;
    }

    @Override
    public Map<String,Object> compute(Map<String, Object> data) {
        String userId = Util.null2String(data.get("userId"));
        List<Map<String, Object>> vocationList = (List<Map<String, Object>>) data.get("vocationList");
        String releaseDate = Util.null2String(data.get("releaseDate"));
        if ("".equals(releaseDate)) {
            releaseDate = DateUtil.getCurrentTime();
        }
        String cover = Util.null2String(data.get("cover"));
        //起算日期
        String qsrq = Util.null2String(vocationList.get(0).get("qsrqValue"));
        //调整起算日期策略
        String dzqsrq= Util.null2String(vocationList.get(0).get("dzqsrq"));
        //分割日期
        String fgrq= Util.null2String(vocationList.get(0).get("fgrq"));
        //额度依据
        String edyj= Util.null2String(vocationList.get(0).get("edyj"));
        data.put("isOneTime","1");

        if ("".equals(qsrq)){
            return Maps.newHashMap();
        }

        qsrq = VocationCommonUtil.getAfterAdjustTime(qsrq,dzqsrq,fgrq);
        for (Map<String, Object> vocationMap : vocationList) {
            vocationMap.put("qsrqValue", qsrq);
        }
        //额度有效期间单位
        String edyxq = Util.null2String(vocationList.get(0).get("edyxq"));

        Map<String, Object> insertHoliDayParam = Maps.newHashMap();

        List<Map<String,Object>> dataList = Lists.newArrayList();

        //实际休息时长
        double restTime = 0;
        SinglePaymentInFullWay singlePaymentInFullWay = new SinglePaymentInFullWay();
        if ("0".equals(edyj) || "2".equals(edyj) || "4".equals(edyj)){
            Map<String, Object> maxMap = vocationList.stream().max(Comparator.comparing(e->Double.valueOf(e.get("jgsc1").toString()).intValue())).get();
            int maxjgsc1 = Double.valueOf(maxMap.get("jgsc1").toString()).intValue();
            insertHoliDayParam = getInsertHoliDayParam(vocationList.get(0), userId, qsrq, releaseDate,maxjgsc1);
            dataList = VocationCommonUtil.ifexist("uf_jcl_kq_jqye", insertHoliDayParam);
            //已存在数据且不覆盖
            if (dataList.size()>0 && !"1".equals(cover)) {
                return insertHoliDayParam;
            }

            if (edyxq.equals(StartingUnitEnum.NATURAL_YEAR.getKey())){
                //自然年
                for (int i=0;i<=maxjgsc1;i++){
                    String executeTime = DateUtil.nextYear(qsrq,i,DateUtil.yyyyMMdd);
                    data.put("releaseDate",executeTime);
                    Map<String,Object> map = singlePaymentInFullWay.compute(data);
                    double ktsc = map.get("ktsc") == null?0:Double.valueOf(map.get("ktsc").toString());
                    restTime = restTime + ktsc;
                }
            }else if (edyxq.equals(StartingUnitEnum.STARTING_YEAR.getKey())){
                //起算年
                for (int i=0;i<=maxjgsc1;i++){
                    String executeTime = DateUtil.nextYear(qsrq,i,DateUtil.yyyyMMdd);
                    data.put("releaseDate",executeTime);
                    Map<String,Object> map = singlePaymentInFullWay.compute(data);
                    double ktsc = map.get("ktsc") == null?0:Double.valueOf(map.get("ktsc").toString());
                    restTime = restTime + ktsc;
                }

            }else if (edyxq.equals(StartingUnitEnum.NATURAL_MONTH.getKey())){
                //自然月
                for (int i=0;i<=maxjgsc1;i++){
                    String executeTime = DateUtil.nextMonth(qsrq,i,DateUtil.yyyyMMdd);
                    data.put("releaseDate",executeTime);
                    Map<String,Object> map = singlePaymentInFullWay.compute(data);
                    double ktsc = map.get("ktsc") == null?0:Double.valueOf(map.get("ktsc").toString());
                    restTime = restTime + ktsc;
                }


            }else if (edyxq.equals(StartingUnitEnum.STARTING_MONTH.getKey())){
                //起算月
                for (int i=0;i<=maxjgsc1;i++){
                    String executeTime = DateUtil.nextMonth(qsrq,i,DateUtil.yyyyMMdd);
                    data.put("releaseDate",executeTime);
                    Map<String,Object> map = singlePaymentInFullWay.compute(data);
                    double ktsc = map.get("ktsc") == null?0:Double.valueOf(map.get("ktsc").toString());
                    restTime = restTime + ktsc;
                }


            }else if (edyxq.equals(StartingUnitEnum.NATURAL_WEEK.getKey())){
                //自然周
                for (int i=0;i<=maxjgsc1;i++){
                    String executeTime = DateUtil.AfterDay(qsrq,i*7);
                    data.put("releaseDate",executeTime);
                    Map<String,Object> map = singlePaymentInFullWay.compute(data);
                    double ktsc = map.get("ktsc") == null?0:Double.valueOf(map.get("ktsc").toString());
                    restTime = restTime + ktsc;
                }


            }else if (edyxq.equals(StartingUnitEnum.STARTING_WEEK.getKey())){
                //起算周
                for (int i=0;i<=maxjgsc1;i++){
                    String executeTime = DateUtil.AfterDay(qsrq,i*7);
                    data.put("releaseDate",executeTime);
                    Map<String,Object> map = singlePaymentInFullWay.compute(data);
                    double ktsc = map.get("ktsc") == null?0:Double.valueOf(map.get("ktsc").toString());
                    restTime = restTime + ktsc;
                }

            }else if (edyxq.equals(StartingUnitEnum.NATURAL_DAY.getKey())){
                //自然天
                for (int i=0;i<=maxjgsc1;i++){
                    String executeTime = DateUtil.AfterDay(qsrq,i);
                    data.put("releaseDate",executeTime);
                    Map<String,Object> map = singlePaymentInFullWay.compute(data);
                    double ktsc = map.get("ktsc") == null?0:Double.valueOf(map.get("ktsc").toString());
                    restTime = restTime + ktsc;
                }
            }
        }else {
            insertHoliDayParam = singlePaymentInFullWay.compute(data);
            dataList = VocationCommonUtil.ifexist("uf_jcl_kq_jqye", insertHoliDayParam);
            //已存在数据且不覆盖
            if (dataList.size()>0 && !"1".equals(cover)) {
                return insertHoliDayParam;
            }
            double ktsc = insertHoliDayParam.get("ktsc") == null?0:Double.valueOf(insertHoliDayParam.get("ktsc").toString());
            restTime = ktsc;
        }
        insertHoliDayParam.put("ktsc", restTime);
        VocationCommonUtil.updateHolidayBalance("uf_jcl_kq_jqye", insertHoliDayParam,dataList,edyxq);

        return insertHoliDayParam;
    }

    /**
     * 获得初始参数
     * @param vocationMap
     * @param userId
     * @param qsrq
     * @param releaseDate
     * @return
     */
    public Map<String,Object> getInsertHoliDayParam(Map<String,Object> vocationMap,String userId,String qsrq,String releaseDate,int maxjgsc1){
        Map<String,Object> param = Maps.newHashMap();

        //额度有效期间单位
        String edyxq = Util.null2String(vocationMap.get("edyxq"));

        param.put("lyid",vocationMap.get("dataid"));
        param.put("jqid",vocationMap.get("jb"));
        param.put("ygid",userId);
        param.put("qsrq",qsrq);
        param.put("ffsj",releaseDate);
        if (edyxq.equals(StartingUnitEnum.NATURAL_YEAR.getKey())){
            //自然年
            String sxrq = qsrq.split("-")[0]+"-01-01";

            String endSxrq = DateUtil.nextYear(sxrq,maxjgsc1,DateUtil.yyyy);
            param.put("sxrq",sxrq);
            param.put("jzrq",endSxrq+"-12-31");

        }else if (edyxq.equals(StartingUnitEnum.STARTING_YEAR.getKey())){
            //起算年
            param.put("sxrq",qsrq);
            String endjzrq = DateUtil.nextYear(qsrq,maxjgsc1+1,DateUtil.yyyyMMdd);

            param.put("jzrq",DateUtil.beforeDay(endjzrq,1));

        }else if (edyxq.equals(StartingUnitEnum.NATURAL_MONTH.getKey())){
            //自然月
            String sxrq = qsrq.split("-")[0]+"-"+qsrq.split("-")[1]+"-01";
            String endSxrq = DateUtil.nextMonth(sxrq,maxjgsc1,DateUtil.yyyyMM);
            param.put("sxrq",sxrq);
            int days = DateUtil.getDays(endSxrq, Calendar.DAY_OF_MONTH);
            param.put("jzrq",endSxrq+"-"+days);

        }else if (edyxq.equals(StartingUnitEnum.STARTING_MONTH.getKey())){
            //起算月
            param.put("sxrq",qsrq);
            String endjzrq = DateUtil.nextMonth(qsrq,maxjgsc1+1,DateUtil.yyyyMMdd);
            param.put("jzrq",DateUtil.beforeDay(endjzrq,1));

        }else if (edyxq.equals(StartingUnitEnum.NATURAL_WEEK.getKey())){
            //自然周
            int dayOfWeek = TimeUtil.getDayOfWeek(qsrq);
            if (dayOfWeek == 0){
                dayOfWeek = 7;
            }
            String sxrq = DateUtil.beforeDay(qsrq,dayOfWeek-1);
            String endSxrq = DateUtil.AfterDay(sxrq,maxjgsc1*7);
            param.put("sxrq",sxrq);
            param.put("jzrq",DateUtil.AfterDay(endSxrq,6));
        }else if (edyxq.equals(StartingUnitEnum.STARTING_WEEK.getKey())){
            //起算周
            param.put("sxrq",qsrq);
            param.put("jzrq",DateUtil.AfterDay(qsrq,7*(maxjgsc1+1)-1));

        }else if (edyxq.equals(StartingUnitEnum.NATURAL_DAY.getKey())){
            //自然天
            param.put("sxrq",qsrq);
            param.put("jzrq",DateUtil.AfterDay(qsrq,maxjgsc1));
        }
        return param;
    }
}
